//
//  PlayGroundViewController.m
//  cityStar
//
//  Created by wangjia on 15/3/28.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "PlayGroundViewController.h"
#import "playCategory.h"
#import "playCategoryNews.h"
#import "playGroundCateCell.h"
#import "playGroundFootCell.h"
#import "playGroundFourCell.h"
#import "CommonUtil.h"
#import "CityDynamicDetailViewController.h"

@interface PlayGroundViewController (){
    NSArray *aryContent;
    int intSelect;
}

@end

@implementation PlayGroundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addBackButton];
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 0;
    self.navigationItem.title = @"玩转城市";
    
    intSelect = 0;
    
    [self loadBanner];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadBanner{
    NSString *strUrl = [@"" stringByAppendingFormat:@"%@/%d/%@",KinterFace_bannerShow,2,@"122"];
    
    [HttpRequestManager getMethod:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            NSArray *aryBanner = [NewsBanner getListArray:response[@"value"]];
            if (aryBanner.count > 0) {
                self.vHeader.aryImage = aryBanner;
            }
        }
    }];
}

- (void)loadData{
    NSString *strUrl = [@"" stringByAppendingFormat:@"%@/%@",KinterFace_RaiderNews,@"122"];
    
    [HttpRequestManager getMethod:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            aryContent = [playCategory getListArray:response[@"value"]];
            
            if (aryContent.count > 0) {
                playCategory *value = aryContent[0];
                [self.btnForst setTitle:value.category_name forState:UIControlStateNormal];
                self.btnForst.tag = 0;
                [self.btnForst addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            }
            
            if (aryContent.count > 1) {
                playCategory *value = aryContent[1];
                [self.btnSecond setTitle:value.category_name forState:UIControlStateNormal];
                self.btnSecond.tag = 1;
                [self.btnSecond addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            }
            
            if (aryContent.count > 2) {
                playCategory *value = aryContent[2];
                [self.btnThird setTitle:value.category_name forState:UIControlStateNormal];
                self.btnThird.tag = 2;
                [self.btnThird addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            }
            
            [self.tableView reloadData];
            
        }
    }];
}

- (IBAction)btnAction:(UIButton *)sender{
    intSelect = (int)sender.tag;
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.1;
    }
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 44;
    }
    return 150;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        if (aryContent.count > intSelect) {
            playCategory *value = aryContent[intSelect];
            return value.categoryNews.count;
        }else{
            return 0;
        }
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        static NSString *cellIdentifier = @"CellFour";
        playGroundFourCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        return cell;
    }
    if (indexPath.section == 2) {
        static NSString *cellIdentifier = @"CellFoot";
        playGroundFootCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        return cell;
    }
    if (indexPath.section == 0) {
        static NSString *cellIdentifier = @"CellCategory";
        playGroundCateCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        playCategory *value = aryContent[intSelect];
        playCategoryNews *news = value.categoryNews[indexPath.row];
        cell.lblContent.text = news.name;
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CityDynamicDetailViewController *viewController = (CityDynamicDetailViewController *)[CommonUtil getVCWithSbIden:@"Index" identify:@"webview"];
    
    playCategory *value = aryContent[intSelect];
    playCategoryNews *news = value.categoryNews[indexPath.row];
    
    viewController.strID = news.id_i;
    
    [self.navigationController pushViewController:viewController animated:YES];
}


@end
